using System;
using System.Configuration;
using System.Threading;

namespace TKH.Biztalk.Diagnostics
{
    /// <summary>
    /// 
    /// </summary>
    public class XmlTraceListenerConfiguration : ConfigurationSection
    {
        //<TKH.TraceListener maxFileSizeBytes="10000000" maxDurationMinutes="1440"/>
        internal const string SectionName = "TKH.TraceListener";

        static XmlTraceListenerConfiguration configuration = null;
        static object _configurationLock = new object();
        static int _recursionGuard = 0;

        
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("maxFileSizeBytes", DefaultValue = 10485760)]
        public int MaxFileSizeBytes
        {
            get { return (int)this["maxFileSizeBytes"]; }
        }
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationProperty("maxDurationMinutes", DefaultValue = 1440)]
        public int MaxDurationMinutes
        {
            get { return (int)this["maxDurationMinutes"]; }
        }

        #region Current static method
        /// <summary>
        /// 
        /// </summary>
        static public XmlTraceListenerConfiguration Current
        {
            get
            {
                if (XmlTraceListenerConfiguration.configuration == null)
                {
                    lock (_configurationLock)
                    {
                        if (XmlTraceListenerConfiguration.configuration == null)
                        {
                            XmlTraceListenerConfiguration configuration = null;
                            if (Interlocked.CompareExchange(ref _recursionGuard, 1, 0) > 0)
                            {
                                throw new ConfigurationErrorsException("LoggingConfigurationNotInitialized");
                            }
                            try
                            {
                                if (configuration == null)
                                {
                                    configuration = (XmlTraceListenerConfiguration)ConfigurationManager.GetSection(XmlTraceListenerConfiguration.SectionName);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new ConfigurationErrorsException(ex.Message);
                            }
                            if (configuration == null)
                            {
                                configuration = new XmlTraceListenerConfiguration();
                            }
                            XmlTraceListenerConfiguration.configuration = configuration;
                        }
                    }
                }
                return XmlTraceListenerConfiguration.configuration;
            }
        }
        #endregion
    }

}
